package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.MonthStoreBean;
import com.emip.dao.MonthStoreDAO;



@Repository("monthStoreDAO")
public class MonthStoreDAOImpl extends StoreDAO implements MonthStoreDAO {

	public List<MonthStoreBean> getByTimespan(int startYear, int startMonth, 
			int endYear, int endMonth) {
		return this.getByTimespan(startYear, startMonth, endYear, endMonth, null, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<MonthStoreBean> getByTimespan(int startYear, int startMonth, 
			int endYear, int endMonth, String[] items, String siteId) {
		String hql = getMonthHQL(startYear, startMonth, endYear, endMonth, items, siteId);
		hql += ", entity.item.id";
		return this.getHibernateTemplate().find(hql);
	}
	
	public MonthStoreBean merge(MonthStoreBean store) {
		return this.getHibernateTemplate().merge(store);
	}

	@SuppressWarnings("unchecked")
	public long getCount(int startYear, int startMonth, int endYear,
			int endMonth, String[] items, String siteId) {
		String hql = "select distinct entity.year, entity.month " + 
				getMonthHQL(startYear, startMonth, endYear, endMonth, items, siteId);
		List<Object> objs = getHibernateTemplate().find(hql);
		if (objs != null) return objs.size();
		return 0;
	}

	@SuppressWarnings("unchecked")
	public String getLastDate(String itemId, String siteId) {
		String hql = "select max(m.year) from MonthStoreBean m where 1 = 1";
		if (itemId != null) {
			hql += " and m.item.id = '" + itemId + "'";
		}
		if (siteId != null) {
			hql += " and m.site.id = '" + siteId + "'";
		}
		List<Object> list = getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0 && list.get(0) != null) {
			String year =  list.get(0).toString();
			hql = "select max(m.month) from MonthStoreBean m where m.year = '" + year + "'";
			if (itemId != null) {
				hql += " and m.item.id = '" + itemId + "'";
			}
			if (siteId != null) {
				hql += " and m.site.id = '" + siteId + "'";
			}
			list = getHibernateTemplate().find(hql);
			if (list != null && list.size() > 0) {
				return year + " " + list.get(0);
			}
		}
		return null;
	}

	public List<MonthStoreBean> getByTimespan(int startYear, int startMonth,
			int endYear, int endMonth, String[] items, String siteId, int page,
			int size) {
		return getThPage(MonthStoreBean.class, "month", startYear, startMonth, endYear, endMonth, items, siteId, page, size);
	}

	public Object[] getStatistics(int startYear, int startMonth, int endYear,
			int endMonth, String itemIds, String siteId) {
		long count = this.getCount(startYear, startMonth, endYear, endMonth, new String[]{itemIds}, siteId);
		if (count == 0) return null;
		String[] result = this.getThStatistic("Month", "month", startYear, startMonth, endYear, endMonth, new String[]{itemIds}, siteId);
		result[3] = Integer.parseInt(result[3])*100/count + "%";
		return result;
	}

	public MonthStoreBean findByMonth(String year, String month, String siteId,
			String itemId) {
		int iyear = Integer.parseInt(year);
		int imonth = Integer.parseInt(month);
		List<MonthStoreBean> months = this.getByTimespan(iyear, imonth, iyear, imonth, new String[]{itemId}, siteId);
		if (months != null && months.size() > 0) {
			return months.get(0);
		}
		return null;
	}
}
